package AdminClient;
import java.net.*;
import java.io.*;
import java.util.Scanner;

public class ServerInterface extends Thread{
	
	public boolean done = false;
	Admin_Manager admin_thread;
	Admin_Listener listener_thread;
	AdminInfo admin;
	
	public ServerInterface(AdminInfo ai)
	{
		admin = ai;
	}
	
	public void run() {
		
		admin_thread = new Admin_Manager();
		listener_thread = new Admin_Listener();
		admin_thread.start();
		try{
			sleep(1000);
		} catch (InterruptedException e)
		{
			System.out.println("Server Update Interrupted");
		}
		listener_thread.start();
		while(!done){
			try{
				sleep(100);
			} catch(InterruptedException e)
			{
				System.out.println("Server Communications Interrupted");
			}
		}
		System.out.println("Exiting Server Interface");
		listener_thread.stop();
		try{
			admin_thread.sleep(5000);
		} catch(InterruptedException e)
		{
			System.out.println("Server Interface Exit Interrupted");
		}
		admin_thread.stop();
	}
	
	public class Admin_Listener extends Thread
	{
		Socket listen_socket;
		
		public void run()
		{
			try {
				String host;
				String update;
				try {
					InetAddress here = InetAddress.getLocalHost();
					host = here.getHostName();
					listen_socket = new Socket(host, 8902);
					System.out.println("Connecting Update Listener");
				}catch (UnknownHostException e) {
					System.out.println("Problem with local host\n");
				}

				DataInputStream in_stream = new DataInputStream(listen_socket.getInputStream());
				DataOutputStream out_stream = new DataOutputStream(listen_socket.getOutputStream());
				System.out.println("Update Connection Established");
				
				String message = "";
				sleep(100);
				try {
					System.out.println("out admin name");
					out_stream.writeUTF("admin");
					out_stream.flush();
					
					sleep(100);
					
				} catch (IOException e) {
					System.out.println("Send Message IOException");
				} catch (InterruptedException e) {
					System.out.println("Send Message InterruptException");
				}
				
				sleep(100);
				
				while(!done)
				{
					try {
						System.out.println("LISTENER: Waiting for update message");
						message = in_stream.readUTF();
						if(message == "server is shutting down")
						{
							done = true;
						}
						System.out.println("Update Message: " + message + "\n");
					}catch (Exception e) {
					}
					System.out.println("1");
					admin.update(message);
					sleep(100);
				}
				listen_socket.close();
			}catch (Exception e) {
				System.out.println("Thread Admin_Listener Exception\n");
			}
		}
	}
	
	public class Admin_Manager extends Thread
	{
		Socket admin_socket;
		String message;
		//boolean send_msg = false;
		boolean ready_to_send = false;
		DataInputStream in_stream;
		DataOutputStream out_stream;
		ObjectInputStream obIn;
		ObjectOutputStream obOut;
		
		public String send_message(String msg)
		{
			System.out.println("Server Interface"+msg);
			if(ready_to_send)
			{
				System.out.println("Server Interface2"+msg);
				message = msg;
				String ret_msg = "";
				//send_msg = true;
				try {
					if(!msg.equals("quit"))
					{
						System.out.println("Server Interface3"+msg);
						out_stream.writeUTF(message);
						out_stream.flush();
						System.out.println("Server Interface4"+msg);
					}
					if (message.equals("shutdown"))
						message = "quit";
					//send_msg = false;
					
					sleep(100);
				} catch (IOException e) {
					System.out.println("Send Message IOException");
				} catch (InterruptedException e) {
					System.out.println("Send Message InterruptException");
				}
				
				try {
					if(!message.equals("quit"))
					{
						System.out.println("Server Interface5"+msg);
						ret_msg = in_stream.readUTF();
						System.out.println("Server Interface6"+msg);
						//System.out.println(ret_msg);
					}
				} catch (EOFException e) {
				} catch (IOException e) {
					System.out.println("IO Exception");
					e.printStackTrace();
				}
				
				if(message.equals("quit") || message.equals("shutdown"))
				{
					ret_msg = "quit";
					done = true;
				}
				return ret_msg;
				//sleep(100);
			}
			return "Server Interface Not Ready To Send";
		}
		
		public void run()
		{
			try{
				String host;
				String response;
				try{
					try {
					///	System.out.println("here");
						InetAddress here = InetAddress.getLocalHost();
						host = here.getHostName();
						//host = "172.25.44.65";
						admin_socket = new Socket(host, 8901);
						System.out.println("Socket created: connecting to server on " + host);
					} catch (UnknownHostException e) {
						System.out.println("Problem with local host\n");
					}
					//System.out.println("A");
					in_stream = new DataInputStream(admin_socket.getInputStream());
					///System.out.println("B");
					out_stream = new DataOutputStream(admin_socket.getOutputStream());
					//System.out.println("C");
					//obIn = new ObjectInputStream(admin_socket.getInputStream());
					//System.out.println("D");
					//obOut = new ObjectOutputStream(admin_socket.getOutputStream());
					//System.out.println("E");
					sleep(100);
					
					ready_to_send = true;
					System.out.println("Ready To Send");
					
					while(!done)
					{
						sleep(500);
					}
					/*try {
						System.out.println("ADMIN_MANAGER: Waiting for confirm");
						message = in_stream.readUTF();
						System.out.println("ADMIN_MANAGER: Confirmation");
						System.out.println(message);
					} catch (EOFException e) {
					} catch (IOException e) {
						System.out.println("IO Exception");
						e.printStackTrace();
					}*/
					
					//message = "";
					//response = "";
					
					/*while (!message.equals("quit"))
					{
						sleep(100);
						//send_message("");
						while(!send_msg){
							sleep(100);
						}
						//System.out.print("# ");
						//Scanner scan = new Scanner(System.in);
						//if (scan.hasNextLine()) {
						//	message = scan.nextLine();
						//	System.out.println("Sending message: " + message + " to server");
						//}
						out_stream.writeUTF(message);
						out_stream.flush();
						if (message.equals("shutdown"))
							message = "quit";
						//send_msg = false;
						
						sleep(100);
						
						try {
							if(message != "quit")
							{
								response = in_stream.readUTF();
								System.out.println(response);
							}
						} catch (EOFException e) {
						} catch (IOException e) {
							System.out.println("IO Exception");
							e.printStackTrace();
						}
						sleep(100);
					}*/
				}catch (IOException e) {
					System.out.println("Network Exception");
					e.printStackTrace();
				}
				admin_socket.close();
				System.out.println("Connection closed");
				done = true;
			} catch (Exception e) {
				System.out.println("Exception");
			}
		}	
	}
}


